package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kobeliu.api.feign.WorkMicroServiceFeign;
import com.kobeliu.enums.AggsUserType;
import com.kobeliu.enums.Sex;
import com.kobeliu.enums.ShowWhichName;
import com.kobeliu.enums.UserRole;
import com.kobeliu.mapper.UsersMapper;
import com.kobeliu.pojo.Users;
import com.kobeliu.pojo.eo.AggsUserEO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.service.UsersService;
import com.kobeliu.utils.DesensitizationUtil;
import com.kobeliu.utils.LocalDateUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UsersService {

    //注入feign客户端
    @Autowired
    private WorkMicroServiceFeign workMicroServiceFeign;


    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ElasticsearchRestTemplate esTemplate;



    /**
     * 根据手机号查询数据
     * @param mobile
     * @return
     */
    @Override
    public Users queryByMobile(String mobile) {

        return usersMapper.selectOne(new QueryWrapper<Users>().eq("mobile",mobile));
    }

    /**
     * 创建用户数据
     * @param mobile
     * @return
     */
    @GlobalTransactional
    @Transactional
    @Override
    public Users createUsers(String mobile) {

        Users users = new Users();
        users.setMobile(mobile);
        //脱敏处理
        users.setNickname("用户"+ DesensitizationUtil.commonDisplay(mobile));
        users.setRealName("用户"+ DesensitizationUtil.commonDisplay(mobile));
        users.setShowWhichName(ShowWhichName.nickname.type);

        users.setSex(Sex.secret.type);
        users.setFace("");
        users.setEmail("");

        LocalDate birthday = LocalDateUtils.parseLocalDate("1980-01-01 11:11:11",LocalDateUtils.DATETIME_PATTERN);
        users.setBirthday(birthday);

        users.setCountry("中国");
        users.setProvince("");
        users.setCity("");
        users.setDistrict("");
        users.setDescription("这世界有10种人，一种懂二进制，一种不懂。");

        //默认使用当天日期
        users.setStartWorkDate(LocalDate.now());
        users.setPosition("Vegetable Bird");
        users.setRole(UserRole.CANDIDATE.type);
        users.setHrInWhichCompanyId("");

        users.setCreatedTime(LocalDateTime.now());
        users.setUpdatedTime(LocalDateTime.now());

        usersMapper.insert(users);


        //此处发起一个远程调用，使初始化简历 （/resume/init）
        //微服务远程调用
        //openFeign 声明式接口形式的进行调用
        //不使用openFeign的时候 我们使用http请求，或者restTemplate的形式发起请求，繁琐
        //微服务通过注册中心，调用方获取提供方的清单，负载均衡选择一个提供方发送请求
        GraceJSONResult result = workMicroServiceFeign.init(users.getId());

        //ES中创建一个集中的用户数据
        AggsUserEO eo = new AggsUserEO();
        eo.setUserId(users.getId());
        eo.setSex(users.getSex());
        eo.setNickname(users.getNickname());
        eo.setType(AggsUserType.USER.type);
        eo.setResumeId(result.getData().toString());
        eo.setCreateDate(LocalDateUtils.format(LocalDateTime.now(),LocalDateUtils.DATE_PATTERN));

        esTemplate.save(eo);

        return users;
    }

    @Override
    public Users getById(String id) {
        return usersMapper.selectById(id);
    }
}
